package com.arcvideo.acsconnect.helper;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.arcvideo.tyingyitonginhouse.R;
import com.arcvideo.acsconnect.app.AcsConnectApplication;
import com.framework.core.utils.AppUtil;
import com.framework.core.utils.BitmapUtil;
import com.framework.core.utils.ToastUtil;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;

/**
 * @author shenmengchao
 * @version 1.0.0
 * @date 2017/11/1
 * @description
 */

public class WXClient {

    public static WXClient mInstance;

    private WXClient() {
    }

    public static WXClient getInstance() {
        if (null == mInstance) {
            mInstance = new WXClient();
        }
        return mInstance;
    }

    public boolean shareToTimeLine(String sessionId) {
        if (!AcsConnectApplication.mWxApi.isWXAppInstalled()) {
            ToastUtil.showToast("您还未安装微信客户端");
            return false;
        }
        String url = getUrl(sessionId);
        return share(url, "快来参加视频会议" + sessionId, "快来参加视频会议", sessionId, SendMessageToWX.Req.WXSceneTimeline);
    }

    public boolean shareToFriend(String sessionId) {
        if (!AcsConnectApplication.mWxApi.isWXAppInstalled()) {
            ToastUtil.showToast("您还未安装微信客户端");
            return false;
        }
        String url = getUrl(sessionId);
        return share(url, "虹视通消息", "快来参加视频会议,房间号:" + sessionId, sessionId, SendMessageToWX.Req.WXSceneSession);
    }

    private boolean share(String url, String title, String description, String sessionId, int scene) {
        WXWebpageObject webPage = new WXWebpageObject();
        webPage.webpageUrl = url;
        WXMediaMessage msg = new WXMediaMessage(webPage);
        msg.title = title;
        msg.description = description;
        Bitmap bitmap = BitmapFactory.decodeResource(AcsConnectApplication.getInstance().getResources(),
                R.mipmap.ic_launcher);
        msg.thumbData = BitmapUtil.bitmapToByteArray(bitmap);


        SendMessageToWX.Req req = new SendMessageToWX.Req();    //创建一个请求对象
        req.message = msg;  //把msg放入请求对象中
        req.scene = scene;    //设置发送到朋友圈
//        req.scene = SendMessageToWX.Req.WXSceneSession;   //设置发送给朋友
        req.transaction = sessionId;  //这个tag要唯一,用于在回调中分辨是哪个分享请求
        boolean b = AcsConnectApplication.mWxApi.sendReq(req);   //如果调用成功微信,会返回true
        return b;
    }

    public static void wxLogin() {
        if (!AcsConnectApplication.mWxApi.isWXAppInstalled()) {
            ToastUtil.showToast("您还未安装微信客户端");
            return;
        }
        final SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "diandi_wx_login";
        AcsConnectApplication.mWxApi.sendReq(req);
    }

    private String getUrl(String sessionId) {
        String versionCode = AppUtil.getVersionName(AcsConnectApplication.getInstance());
        String url = String.format("http://static.hongshiyun.net/Director/mshare.html?sid=%s&versionCode=%s",
                sessionId, versionCode);
        return url;
    }
}
